package com.yanqu.road.server.cmd.banquet.party;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.banquet.UserCrossBanquetData;
import com.yanqu.road.entity.enums.eBanquetType;
import com.yanqu.road.pb.banquet.BanquetProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.relatives.RelativesModule;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.banquet.BanquetMgr;
import com.yanqu.road.server.manger.banquet.CrossBanquetMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.pb.BanquetPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;

import java.util.Map;
import java.util.Set;

//@Cmd(code = GameProtocol.S_PARTY_LIST,desc = "")
@Cmd(code = Protocol.S_BANQUET_LIST, desc = "请求宴会列表")
public class GetPartyListCmd extends AbstractCommand {
    private int banquetType;
    private int type;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        BanquetProto.BanquetListReq reqMsg = BanquetProto.BanquetListReq.parseFrom(packet.getBytes());
        banquetType = reqMsg.getBanquetType();
        type = reqMsg.getType();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        BanquetProto.BanquetListResp.Builder respMsg = BanquetProto.BanquetListResp.newBuilder();
        if(banquetType == eBanquetType.ServerNormalBanquet.getValue() || banquetType == eBanquetType.ServerFeastBanquet.getValue()){
            BanquetProto.CrossBanquetListReqMsg.Builder req = BanquetProto.CrossBanquetListReqMsg.newBuilder();
            req.setType(banquetType);
            Set<Long> relativeList = player.getModule(RelativesModule.class).getRelativesId();
            req.addAllRelativeList(relativeList);
            player.sendPacket(CrossProtocol.C_CROSS_PARTY_LIST, req);
            return;
        }
       if(banquetType == eBanquetType.CrossBanquet.getValue()){
            Map<String, UserCrossBanquetData> crossBanquets= CrossBanquetMgr.getBanquetOpen(eBanquetType.CrossBanquet.getValue());
            respMsg = BanquetPb.parseBanquetListRespMsgCross(player,crossBanquets);
        }else if(banquetType == eBanquetType.CrossMogulBirthdayBanquet.getValue()){
           Map<String, UserCrossBanquetData> crossBanquets= CrossBanquetMgr.getBanquetOpen(player,eBanquetType.CrossMogulBirthdayBanquet.getValue());
           respMsg = BanquetPb.parseBanquetListRespMsgCross(player,crossBanquets);
        }else if(banquetType == eBanquetType.CrossUserBanquet.getValue()){
           ActivityInfo crbAcInfo = NormalActivityMgr.getRunningCrossBanquetRankActivity();
           if (crbAcInfo == null) {
               respMsg.setAcId(0);
           }else {
               Map<String, UserCrossBanquetData> crossBanquets= CrossBanquetMgr.getBanquetOpen(player,eBanquetType.CrossUserBanquet.getValue());
               respMsg = BanquetPb.parseBanquetListRespMsgCross(player,crossBanquets);
               //要知道每个宴会开的人是不是设置了自动回宴会，要去跨服走一趟
               respMsg.setRet(ret);
               respMsg.setAcId(crbAcInfo.getActivityId());
               YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.C_CROSS_BANQUET_AUTO_LIST, respMsg);
               GamePlayerMgr.sendPacket(player.getUserId(), pbMsg);
               return;
           }
        }else{
            respMsg = BanquetPb.parseBanquetListRespMsg(player,BanquetMgr.getBanquetOpen(player,banquetType,false));
        }

        respMsg.setRet(ret);
        player.sendPacket(Protocol.U_BANQUET_LIST, respMsg);

    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
